package view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import modle.Country;

import javax.swing.JButton;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import java.awt.Toolkit;
import java.awt.Color;

public class Start extends JFrame {

	private JPanel contentPane;
	private JButton btnCustomer;
	private JButton btnAdmin;
	private JComboBox comboBox;
	private List<Country> listCountries;
	private Locale locale;
	ResourceBundle rd;						// 资源束包含区域特定的对象。
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Start frame = new Start();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Start() {
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setIconImage(Toolkit.getDefaultToolkit().getImage(Start.class.getResource("/img/background.jpg")));
		
		//将语言资源包都放在文件夹中
		
		setTitle("Welcome to Food Corner");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 698, 451);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		btnCustomer = new JButton("Customer");			//这是一个顾客按钮（按下去）
		btnCustomer.setFont(new Font("微软雅黑", Font.BOLD, 19));
		btnCustomer.setBounds(240, 167, 135, 42);
		contentPane.add(btnCustomer);
		btnCustomer.addActionListener(new ActionListener(){			//在按钮上加上监听器
			//单击按钮执行的方法：打开顾客登录系统
			public void actionPerformed(ActionEvent e) {
				new Customer_Login().setVisible(true);				//new 出一个新页面
				dispose();
			}
		});
		
		btnAdmin = new JButton("Admin");
		btnAdmin.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 19));
		btnAdmin.setBounds(240, 249, 135, 42);
		contentPane.add(btnAdmin);
		btnAdmin.addActionListener(new ActionListener(){
			//单击按钮执行的方法：打开管理员登录系统
			public void actionPerformed(ActionEvent e) {
				new Admin_Login().setVisible(true);
				dispose();
			}
		});
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	???   Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					
					//rd到底有什么用
					
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(568, 13, 98, 32);
		contentPane.add(comboBox);
		
		lblNewLabel = new JLabel("LOGIN");
		lblNewLabel.setFont(new Font("Dialog", Font.ITALIC, 33));
		lblNewLabel.setBounds(244, 41, 162, 70);
		contentPane.add(lblNewLabel);
		
		lblNewLabel_1 = new JLabel();
		lblNewLabel_1.setBounds(0, 0, 680, 405);
		lblNewLabel_1.setIcon(new ImageIcon(Start.class.getResource("/img/background.jpg")));
		contentPane.add(lblNewLabel_1);
		
		fillCountries();
	}
	
	private ImageIcon ResizeImage(String imgpath, JLabel label) {
		// TODO Auto-generated method stub
		ImageIcon img1 = new ImageIcon(Start.class.getResource(imgpath));
		Image img2 = img1.getImage();
		Image img3 = img2.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
		ImageIcon img4 = new ImageIcon(img3);
		return img4;
	}

	private void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected void changeLanguage() {
		// TODO Auto-generated method stub
		btnCustomer.setText(rd.getString("btnCustomer"));
		btnAdmin.setText(rd.getString("btnAdmin"));
	}
}
